package com.spit.fammo.adapter;

import android.content.Context;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

import com.spit.fammo.R;
import com.spit.fammo.util.StatusHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @Author nike
 * @Date 2023/6/9 15:03
 * @Description
 */
public class AssetTextAdapter2 extends BaseAdapter {

    private Context act;
    private JSONArray jsonArray;

    public AssetTextAdapter2(Context act, JSONArray jsonArray) {
        this.act = act;
        this.jsonArray = jsonArray;
    }

    @Override
    public int getCount() {
        return jsonArray.length();
    }

    @Override
    public Object getItem(int i) {
        return jsonArray.optJSONObject(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = convertView.inflate(act, R.layout.i_text, null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {
            JSONObject object = jsonArray.getJSONObject(i);
            String title = object.optString("title");
            String content = object.optString("content");
            String keys = object.optString("keys");
            viewHolder.tv_title.setText(title);
            viewHolder.tv_content.setTextColor(ContextCompat.getColor(act, R.color.gray_c9c9ca));

            try {
                if (keys.equals("cerstatus")){
                    viewHolder.tv_content.setText(StatusHelper.INSTANCE.getCerstatusString(act, Integer.valueOf(content)));
                }else if (keys.equals("statusid")){
                    viewHolder.tv_content.setText(StatusHelper.INSTANCE.getStatusString(act, Integer.valueOf(content)));
                    viewHolder.tv_content.setTextColor(Integer.valueOf(content) == 2 ? ContextCompat.getColor(act, R.color.colorPrimaryDark) : ContextCompat.getColor(act, R.color.red_ff0000));
                }else if (keys.equals("certUrl")){
                    String html = "<a href='" + content + "'>" + content + "</a>";
                    viewHolder.tv_content.setText(Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY));
                    viewHolder.tv_content.setMovementMethod(LinkMovementMethod.getInstance());
                    viewHolder.tv_content.setTextColor(ContextCompat.getColor(act, R.color.blue_3700B3));
                }else if (keys.equals("scanStatus")){
                    viewHolder.tv_content.setText(StatusHelper.INSTANCE.getScanStatusString(act, Integer.valueOf(content)));
                }
                else {
                    viewHolder.tv_content.setText(content);
                }
            } catch (NumberFormatException e) {
                viewHolder.tv_content.setText("Invalid status");
            }

        } catch (JSONException e) {
            throw new RuntimeException("InventoryDesc2Adapter go go go");
        }
        return convertView;
    }

    class ViewHolder{

        AppCompatTextView tv_title;
        AppCompatTextView tv_content;

        public ViewHolder(View convertView) {
            tv_title = convertView.findViewById(R.id.tv_title);
            tv_content = convertView.findViewById(R.id.tv_content);
        }

    }

}
